﻿using SD.Infrastructure.EntityBase;
using System;
using System.Collections.Generic;

namespace SlamDunk.MES.Domain.Entities.AcquisitionContext
{
    /// <summary>
    /// 采集会话
    /// </summary>
    public class AcquisitionSession : AggregateRootEntity
    {
        #region # 构造器

        #region 00.无参构造器
        /// <summary>
        /// 无参构造器
        /// </summary>
        protected AcquisitionSession()
        {
            //默认值
            this.Activate();

            //初始化导航属性
            this.AcquisitionRecords = new HashSet<AcquisitionRecord>();
        }
        #endregion

        #region 01.创建采集会话构造器
        /// <summary>
        /// 创建采集会话构造器
        /// </summary>
        /// <param name="acquisitionRuleId">采集规则Id</param>
        /// <param name="interval">采集间隔（秒）</param>
        /// <param name="processId">工艺Id</param>
        /// <param name="processSegmentId">工艺段Id</param>
        /// <param name="executionPlanId">作业计划Id</param>
        /// <param name="workOrderId">工单Id</param>
        public AcquisitionSession(Guid acquisitionRuleId, int interval, Guid? processId, Guid? processSegmentId, Guid? executionPlanId, Guid? workOrderId)
            : this()
        {
            this.AcquisitionRuleId = acquisitionRuleId;
            this.Interval = interval;
            this.ProcessId = processId;
            this.ProcessSegmentId = processSegmentId;
            this.ExecutionPlanId = executionPlanId;
            this.WorkOrderId = workOrderId;

            //设置工单Id为采集会话Id
            if (this.WorkOrderId.HasValue)
            {
                base.SetId(this.WorkOrderId.Value);
            }
        }
        #endregion

        #endregion

        #region # 属性

        #region 采集规则Id —— Guid AcquisitionRuleId
        /// <summary>
        /// 采集规则Id
        /// </summary>
        public Guid AcquisitionRuleId { get; private set; }
        #endregion

        #region 采集间隔 —— int Interval
        /// <summary>
        /// 采集间隔（单位：秒）
        /// </summary>
        public int Interval { get; private set; }
        #endregion

        #region 工艺Id —— Guid? ProcessId
        /// <summary>
        /// 工艺Id
        /// </summary>
        public Guid? ProcessId { get; private set; }
        #endregion

        #region 工艺段Id —— Guid? ProcessSegmentId
        /// <summary>
        /// 工艺段Id
        /// </summary>
        public Guid? ProcessSegmentId { get; private set; }
        #endregion

        #region 作业计划Id —— Guid? ExecutionPlanId
        /// <summary>
        /// 作业计划Id
        /// </summary>
        public Guid? ExecutionPlanId { get; private set; }
        #endregion

        #region 工单Id —— Guid? WorkOrderId
        /// <summary>
        /// 工单Id
        /// </summary>
        public Guid? WorkOrderId { get; private set; }
        #endregion

        #region 是否活动 —— bool Active
        /// <summary>
        /// 是否活动
        /// </summary>
        public bool Active { get; private set; }
        #endregion

        #region 激活时间 —— DateTime? ActivatedTime
        /// <summary>
        /// 激活时间
        /// </summary>
        public DateTime? ActivatedTime { get; private set; }
        #endregion

        #region 关闭时间 —— DateTime? ClosedTime
        /// <summary>
        /// 关闭时间
        /// </summary>
        public DateTime? ClosedTime { get; private set; }
        #endregion

        #region 关闭原因 —— string ClosedReason
        /// <summary>
        /// 关闭原因
        /// </summary>
        public string ClosedReason { get; private set; }
        #endregion

        #region 导航属性 - 采集记录集 —— ICollection<AcquisitionRecord> AcquisitionRecords
        /// <summary>
        /// 导航属性 - 采集记录集
        /// </summary>
        public virtual ICollection<AcquisitionRecord> AcquisitionRecords { get; private set; }
        #endregion

        #endregion

        #region # 方法

        #region 追加采集记录 —— void AppendRecord(AcquisitionRecord acquisitionRecord)
        /// <summary>
        /// 追加采集记录
        /// </summary>
        /// <param name="acquisitionRecord">采集记录</param>
        public void AppendRecord(AcquisitionRecord acquisitionRecord)
        {
            this.AcquisitionRecords.Add(acquisitionRecord);
            acquisitionRecord.AcquisitionSession = this;
        }
        #endregion

        #region 激活 —— void Activate()
        /// <summary>
        /// 激活
        /// </summary>
        public void Activate()
        {
            this.ActivatedTime = DateTime.Now;
            this.Active = true;
            base.SavedTime = DateTime.Now;
        }
        #endregion

        #region 关闭 —— void Close(string reason)
        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="reason">原因</param>
        public void Close(string reason)
        {
            this.ClosedReason = reason;
            this.Active = false;
            this.ClosedTime = DateTime.Now;
            base.SavedTime = DateTime.Now;
        }
        #endregion

        #endregion
    }
}
